package com.lansetech.framework.redis.peak.shaving;

import java.util.ArrayList;
import java.util.List;

public class PeakShavingConfig {
    public static final String PEAK_SHAVING_TAG_COMMON = "peakshaving";
    public static final String PEAK_SHAVING_TAG_NORMAL = "peakshaving:normal";

    /**
     * queue的数量
     */
    private int numberOfQueue = 10;

    /**
     * queue的前缀
     */
    private String queuePrefix = "priusoft";

    public PeakShavingConfig(){}

    public PeakShavingConfig(int numberOfQueue){
        this.numberOfQueue = numberOfQueue;
    }

    protected PeakShavingConfig(int numberOfQueue, String queuePrefix) {
        this.numberOfQueue = numberOfQueue;
        this.queuePrefix = queuePrefix;
    }


    public int getNumberOfQueue() {
        return numberOfQueue;
    }

    public String fallInQueueName(String type, int index) {
        index = index % numberOfQueue;
        index = (index + numberOfQueue) % numberOfQueue;
        // 变负的为正的
        return combineQueueName(type, index);
    }

    public String getRedisQueueName(int index) {
        if (index < 0 || index >= numberOfQueue) {
            throw new IllegalArgumentException("请输入有效的下标号");
        }
        return combineQueueName(null, index);
    }

    private String combineQueueName(String type, int index) {
        return queuePrefix + ":" + PeakShavingConfig.PEAK_SHAVING_TAG_NORMAL + ":" + type + ":" + index;
    }

    public List<String> getRedisQueueNames() {
        List<String> names = new ArrayList<String>(numberOfQueue);
        for (int i = 0; i < numberOfQueue; i++) {
            names.add(getRedisQueueName(i));
        }
        return names;
    }

    public String getQueuePrefix() {
        return this.queuePrefix;
    }

}
